package aar.test.tessocr.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import aar.test.tessocr.CapitalDetailsActivity;
import aar.test.tessocr.LanListActivity;
import aar.test.tessocr.Login1Activity;
import aar.test.tessocr.MainActivity;
import aar.test.tessocr.OrderListActivity;
import aar.test.tessocr.PayActivity;
import aar.test.tessocr.PieListActivity;
import aar.test.tessocr.baseApplication.BaseApplication;
import aar.test.tessocr.model.CourierInfoModel;
import aar.test.tessocr.model.DialogCallback;
import aar.test.tessocr.model.LzyResponse;
import aar.test.tessocr.utils.PrefUtils;
import aar.test.tessocr.utils.Urls;
import aar.test.tessocr.utils.Utils;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import clx.tessocr.ocreng.R;
import okgo.OkGo;
import okgo.model.Response;
import okhttp3.RequestBody;

import static aar.test.tessocr.Login1Activity.JSON;

/**
 * Created by sunshine on 2020/7/23.
 */
public class MineFragment extends BaseFragment {

    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.balance)
    TextView balance;
    @BindView(R.id.tv_version)
    TextView tv_version;

    public static MineFragment newInstance() {
        return new MineFragment();
    }

    @Override
    protected void setUpContentView() {
        setContentView(R.layout.fragment_mine);
    }

    @Override
    protected void setUpView(View view) {

    }

    @Override
    protected void setUpComponent() {

    }

    @Override
    protected void setUpData() {
        //userName.setText(BaseApplication.getIntance().getLoginBean().getUsername());
        userName.setText(PrefUtils.getString(getActivity(), "userName", ""));
        try {
            tv_version.setText("版本号: v" + Utils.getVersion(getActivity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick({R.id.capital_details, R.id.order_lis, R.id.balance_recharge, R.id.lan_lis, R.id.balance_withdraw, R.id.ll_switch})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.capital_details:
                startActivity(new Intent(getActivity(), CapitalDetailsActivity.class));
                break;
            case R.id.order_lis:
                //startActivity(new Intent(getActivity(), PieListActivity.class));
                startActivity(new Intent(getActivity(), OrderListActivity.class));
                break;
            case R.id.balance_recharge:
                startActivity(new Intent(getActivity(), PayActivity.class));
                break;
            case R.id.lan_lis:
                startActivity(new Intent(getActivity(), LanListActivity.class));
                break;
            case R.id.balance_withdraw:
                Toast.makeText(getActivity(), "请到公众号提现！", Toast.LENGTH_SHORT).show();
                //startActivity(new Intent(getActivity(), WithdrawActivity.class));
                break;
            case R.id.ll_switch:
                PrefUtils.setString(getActivity(), "userName", "");
                PrefUtils.setString(getActivity(), "password", "");
                PrefUtils.setString(getActivity(), "cabinetCode", "无");
                PrefUtils.setBoolean(getActivity(), "cabinetLoginFlag", false);
                PrefUtils.setString(getActivity(), "cabinetId", "");
                BaseApplication.getIntance().setLoginBean(null);
                BaseApplication.getIntance().setLogin(false);
                startActivity(new Intent(getActivity(), Login1Activity.class));
                getActivity().finish();
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Map<String, Object> map = new HashMap<>();
        map.put("adminid", PrefUtils.getString(getActivity(), "userId", ""));

        Gson gg = new Gson();
        String json = gg.toJson(map);

        RequestBody body = RequestBody.create(JSON, json);
        OkGo.<LzyResponse<CourierInfoModel>>post(Urls.URL_CHARGEINFO)//
                .tag(this)//
                .upRequestBody(body)
                .isMultipart(false)         //强制使用 multipart/form-data 表单上传（只是演示，不需要的话不要设置。默认就是false）
                .execute(new DialogCallback<LzyResponse<CourierInfoModel>>(getActivity()) {
                    @Override
                    public void onSuccess(Response<LzyResponse<CourierInfoModel>> response) {
                        if (null != response.body().data) {
                            CourierInfoModel courierInfoModel = response.body().data;
                            balance.setText(courierInfoModel.getMoney() + "元");
                        }
                    }

                    @Override
                    public void onError(Response<LzyResponse<CourierInfoModel>> response) {
                    }
                });
    }
}
